//
//  Document.m
//  Opper
//
//  Created by Le Dung on 4/19/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "Document.h"

@interface Document ()

@end

@implementation Document
@synthesize letterView;
@synthesize isLetterHiden,isMove;
@synthesize categoryArray,productArray,providerArray,profileArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [StyleClass barButtonItemEdit:self left:NO right:YES];
    if([GetData App].signImage)
    {
        self.signImage.image=[GetData App].signImage;
    }
}

-(void)rightPress
{
    if(!isLetterHiden)
    {
        [letterView setHidden:YES];
        isLetterHiden=1;
    }
    else
    {
        [letterView setHidden:NO];
        isLetterHiden=0;
    }
}
-(void)letterDone
{
    
    [self rightPress];
    
}

-(void)leftPress
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)acceptPress
{
    Signature *page=[[[Signature alloc]init] autorelease];
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
}
-(void)cancelPress
{
    [self.navigationController  popViewControllerAnimated:YES];
}

-(void)buttonTarget
{
    [self.acceptButton addTarget:self action:@selector(acceptPress) forControlEvents:UIControlEventTouchUpInside];
    [self.acceptButton setBackgroundImage:[UIImage imageNamed:@"Green-Black145.png"] forState:UIControlStateNormal];
    
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setBackgroundImage:[UIImage imageNamed:@"Red-Black145.png"] forState:UIControlStateNormal];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buttonTarget];
    [StyleClass createNavi:self andTitle:@"Uw Document"];
    [StyleClass backgroundInblack:self.view];
    [self styleContaint];
    [self createLetterView];
    [self.navigationController shouldAutorotate];
    [self loadData];
    [self createListCollection];
    
}


-(void)loadData
{
    categoryArray =[[NSMutableArray alloc]init];
    providerArray =[[NSMutableArray alloc]init];
    productArray =[[NSMutableArray alloc]init];
    profileArray =[[NSMutableArray alloc]init];
    [self loadCategory];
    [self loadProvider];
    [self loadProfileList];
    
    if(self.isInstane==1)
    {
        UIButton *categoryButton =    (UIButton*)[self.view viewWithTag:100] ;
        
        for(CategoryClass *unit in [GetData App].catagoryArray)
        {
            if([unit.idAutoIncr isEqualToString:self.contractInfo.categoryId])
            {
                [categoryButton setTitle:unit.categoryName forState:UIControlStateNormal];

                break;
            }
        }
        
        
        UIButton *providerButton =    (UIButton*)[self.view viewWithTag:101] ;
        [providerButton setTitle:self.contractInfo.providerName forState:UIControlStateNormal];
        UIButton *productButton =    (UIButton*)[self.view viewWithTag:102] ;
        [productButton setTitle:self.contractInfo.productName forState:UIControlStateNormal];
        
        UIButton *userButton  =    (UIButton*)[self.view viewWithTag:103] ;
        [userButton setTitle:[[self.member.firstName stringByAppendingString:@" "]stringByAppendingString:self.member.lastname] forState:UIControlStateNormal];
        
        UITextField *text =(UITextField*)[self.view viewWithTag:104];
        text.text=self.contractInfo.contractNumber;
    }
}


-(void)loadProfileList
{
    for(Member *unit in [GetData App].profileArray)
    {
        ListObject *listObject =[[ListObject alloc]init];
        listObject.objectName = [[unit.firstName stringByAppendingString:@" "]stringByAppendingString:unit.lastname ];
        listObject.objectId =unit.idMember;
        [self.profileArray addObject:listObject];
        [listObject release];
    }
}
-(void)loadCategory
{
    for(CategoryClass *unit in [GetData App].catagoryArray)
    {
        ListObject *listObject =[[ListObject alloc]init];
        listObject.objectName = unit.categoryName;
        listObject.objectId =unit.idAutoIncr;
        [self.categoryArray addObject:listObject];
        [listObject release];
        
    }
}

-(void)loadProvider
{
    for(ProviderClass *unit in [GetData App].providerArray)
    {
        ListObject *listObject =[[ListObject alloc]init];
        listObject.objectName = unit.providerName;
        listObject.objectId =unit.idAutoIncr;
        [self.providerArray addObject:listObject];
        [listObject release];
        
    }
    
}
-(void )createProductView:(NSString*)categoryId andProviderId:(NSString*)providerId
{
    self.productArray =[GetData productFilter:self.productArray andCategoryId:categoryId andProviderId:providerId];
    ListCollection *product=(ListCollection*)[self.view viewWithTag:202];
    [product removeFromSuperview];
    
    product=[StyleClass listCollection:self.containView andViewLocation:[self.view viewWithTag:102] andData:self.productArray andTagSender:102 ];
    product.tag=202;
    product.delegate=self;
}


-(void)createListCollection
{
    

    ListCollection *category=[StyleClass listCollection:self.letterView andViewLocation:[self.view viewWithTag:100] andData:self.categoryArray andTagSender:100];
        category.delegate=self;
    category.tag=200;

    ListCollection *provider=[StyleClass listCollection:self.letterView andViewLocation:[self.view viewWithTag:101] andData:self.providerArray andTagSender:101];
    provider.delegate=self;
    provider.tag=201;
    
    ListCollection *profile=[StyleClass listCollection:self.letterView andViewLocation:[self.view viewWithTag:103] andData:self.profileArray andTagSender:103];
    profile.delegate=self;
    profile.tag=203;
}

-(void)indexPress:(NSString *)stringRespone andTagRespone:(NSInteger)tagR andIndexObject:(NSString *)idRespone andNameRespone:(NSString *)nameRespone
{

    UIButton *button =(UIButton*)[self.view viewWithTag:tagR];
    [button setTitle:nameRespone forState:UIControlStateNormal];
    
    if(tagR==100||tagR==101)
    {
        if(tagR==100)
        {
            self.idCategory =idRespone;
        }
        if(tagR==101)
        {
            self.idProvider=idRespone;
        }
        
        if(self.idCategory && self.idProvider)
        {
            [self createProductView:self.idCategory andProviderId:self.idProvider];
        }
    }
}



//100<= buttontaglist<=150
//200<= viewtagList <= 250;

-(void)createLetterView
{
    letterView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    [self.containView addSubview:letterView];
    [letterView setBackgroundColor:[UIColor clearColor]];
    
    
    
    UIView *blackView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 210)];
    blackView.backgroundColor =[UIColor colorWithRed:19.0/255 green:19.0/255 blue:19.0/255 alpha:0.9];
    
    [self.letterView addSubview:blackView];
    [blackView release];
    [self letterChosen];
    
}

-(IBAction)listCollectionPress:(id)sender
{
    UIView *view =[self.view viewWithTag:[(UIView *)sender tag]+100];
    if(view.hidden)
    {
        [view setHidden:NO];
    }
    else
    {
        [view setHidden:YES];
    }
}



//buton : category : 100   , provider: 101 ; product : 102
-(void)letterChosen
{
    
    NSArray *titleArray =[NSArray arrayWithObjects:@"Categorie",@"Maatschappij",@"Product",@"Gebruiker", nil];
    
    
    for(int i = 0 ; i< 4 ; i++)
    {
        UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(10, 10+ 40*i, 135, 30);
        button.tag = 100+i;
        
        [button setBackgroundImage:[StyleClass nList] forState:UIControlStateNormal];
        
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:13]];
        button.titleLabel.textColor=[UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:135.0/255];
        
        
        [button addTarget:self action:@selector(listCollectionPress:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.letterView addSubview:button];
        
    }
    
    UITextField *contractNumberField =[[UITextField alloc]initWithFrame:CGRectMake(10, 170, 135, 30)];
    [contractNumberField setBackground:[StyleClass nText]];
    
    contractNumberField.placeholder=@"Polis/contractnummer";
    

    contractNumberField.tag =104;
    [contractNumberField setFont:[UIFont systemFontOfSize:13]];
    contractNumberField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    contractNumberField.contentHorizontalAlignment= UIControlContentHorizontalAlignmentLeft;
    
    contractNumberField.textColor=[UIColor colorWithRed:135.0/255 green:135.0/255 blue:135.0/255 alpha:135.0/255];    
    [self.letterView addSubview:contractNumberField];
    [contractNumberField release];
    contractNumberField.delegate=self;
    UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame =CGRectMake(190, 170, 70, 30);
    [button setTitle:@"Ok" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(letterDone) forControlEvents:UIControlEventTouchUpInside];
    [self.letterView addSubview:button];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(isMove)
    {
        [StyleClass moveDown:self.letterView andYLocation:50];
        isMove=0;
    }
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(!isMove)
    {
        [StyleClass moveUp:self.letterView andYLocation:50];
        isMove=1;
    }
}
-(void)styleContaint
{
    [StyleClass background:self.containView];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [letterView release];
    [_containView release];
    [_cancelButton release];
    [_acceptButton release];
    [categoryArray release];
    [providerArray release];
    [productArray release];
    [_signImage release];
    [super dealloc];
}
@end
